package com.wordsbank.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wordsbank.dao.SettingMapper;
import com.wordsbank.dao.WordMapper;
import com.wordsbank.entity.dto.WordDto;
import com.wordsbank.entity.po.Setting;
import com.wordsbank.entity.po.Word;
import com.wordsbank.service.WordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @AUTHOR : Dragon_Yang
 * @File : WordServiceImpl
 * @DESCRIPTION :
 * @DATE : 2022/3/3 22:20
 */
@Service
public class WordServiceImpl extends ServiceImpl<WordMapper, Word> implements WordService {

    @Autowired
    WordMapper wordMapper;

    @Autowired
    SettingMapper settingMapper;

    @Override
    public boolean addWord(WordDto wordDto) {
        System.out.println(wordDto);
        Word word = new Word();
        word.setEnglish(wordDto.getEnglish());
        word.setChinese(wordDto.getChinese());
        word.setLibrary(wordDto.getLibrary());
        word.setCreateTime(java.lang.System.currentTimeMillis());
        word.setCount(0);
        word.setDelFlag(0);
        int res = wordMapper.insert(word);
        if(res > 0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public List<Word> getWordList(String library) {
        System.out.println(library);
        QueryWrapper<Word> queryWrapper = new QueryWrapper<Word>();
        queryWrapper.eq("del_flag", 0)
                .eq("library", library);
        List<Word> list = wordMapper.selectList(queryWrapper);
        Word[] words = new Word[list.size()];
        list.toArray(words);
        long currentTime = System.currentTimeMillis();
        Arrays.sort(words, (o1, o2) -> {
            if(o1.getCount() > o2.getCount()){
                return -1;
            }else if(o1.getCount() < o2.getCount()){
                return 1;
            }else{
                if(currentTime - o1.getShowTime() >= currentTime - o2.getShowTime()){
                    return -1;
                }else{
                    return 1;
                }
            }
        });
        List<Word> returnList = Arrays.asList(words);
        if(returnList.size() > 30){
            returnList = returnList.subList(0, 30);
        }
        return returnList;
    }

    @Override
    public boolean updateWord(Word word) {
        System.out.println(word);
        word.setCount(word.getCount() + 1);
        int degree = Integer.parseInt(settingMapper.selectOne(new QueryWrapper<Setting>().eq("setkey", "degree")).getSetvalue());
        if(word.getCount() >= degree){
            word.setCount(0);
        }
        word.setShowTime(System.currentTimeMillis());
        int res = wordMapper.updateById(word);
        if(res > 0){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteWord(Word word) {
        System.out.println(word);
        word.setDelFlag(1);
        int res = wordMapper.updateById(word);
        if(res > 0){
            return true;
        }
        return false;
    }


}
